//
//  HWPromoteCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/28.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import Cosmos
class HWPromoteCell: UITableViewCell {
    var bgView:UIView!
    var shadowView:UIView!
    /// 排名
    var numLabel:UILabel!
    /// 游戏图标
    var gameImageView:UIImageView!
    /// 游戏名
    var nameLabel:UILabel!
    /// 类型
    var typeLabel:UILabel!
    /// 推荐指数
    var recommendLabel:UILabel!
    /// 星星
    var starView:CosmosView!
    /// 分成比例
    var scaleLabel:UILabel!
    /// 超高返利
    var rebateLabel:UILabel!
    /// 送满级神装
    var divineLabel:UILabel!
    
    /// 复制推广
    var copyPromoteImageView:UIImageView!
    var copyPromoteLabel:UILabel!
    var copyPromoteButton:UIButton!
    
    /// 二维码
    var qrCodeImageView:UIImageView!
    var qrCodeLabel:UILabel!
    var qrCodeButton:UIButton!
    
    /// 微信
    var wechatImageView:UIImageView!
    var wechatLabel:UILabel!
    var wechatButton:UIButton!
    
    /// qq
    var qqImageView:UIImageView!
    var qqLabel:UILabel!
    var qqButton:UIButton!
    
    /// 朋友圈
    var friendImageView:UIImageView!
    var friendLabel:UILabel!
    var friendButton:UIButton!
    
    var model:HWPromoteGameModel? {
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name
            typeLabel.text = model?.type
            scaleLabel.text = "分成比例：\(model?.fcbl ?? "")"
            scaleLabel.isHidden = true
            rebateLabel.isHidden = true
            divineLabel.isHidden = true
            if model?.biaoqian.count == 1 {
                scaleLabel.text = model?.biaoqian[0]
                scaleLabel.isHidden = false
            } else if model?.biaoqian.count == 2 {
                scaleLabel.text = model?.biaoqian[0]
                rebateLabel.text = model?.biaoqian[1]
                scaleLabel.isHidden = false
                rebateLabel.isHidden = false
            } else if model?.biaoqian.count == 3 {
                scaleLabel.text = model?.biaoqian[0]
                rebateLabel.text = model?.biaoqian[1]
                divineLabel.text = model?.biaoqian[2]
                scaleLabel.isHidden = false
                rebateLabel.isHidden = false
                divineLabel.isHidden = false
            }
            
            starView.rating = model?.point ?? 0
        }
    }
    
    var indexPath:IndexPath? {
        didSet {
            numLabel.text = "\(indexPath!.row + 1)"
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWPromoteCell) {
        let reUse:String = "HWPromoteCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWPromoteCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWPromoteCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12*widthScale
        bgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#C4C4C4", alpha: 0.16).cgColor
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        //        bgView.layer.shadowRadius = 4
        bgView.layer.shadowOpacity = 1
        contentView.addSubview(bgView)
        
        shadowView = UIView()
        shadowView.backgroundColor = .white
        shadowView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#7D7D7D", alpha: 0.12).cgColor
        shadowView.layer.shadowOffset = CGSize(width: 0, height: 4*widthScale)
        //        shadowView.layer.shadowRadius = 4
        shadowView.layer.shadowOpacity = 1
        bgView.addSubview(shadowView)
        
        numLabel = HWLabel.createLabel(title: "1", color: "#FFFFFF", font: 10*widthScale)
        numLabel.font = .systemFont(ofSize: 10*widthScale, weight: .semibold)
        numLabel.layer.cornerRadius = 8*widthScale
        numLabel.backgroundColor = .colorWithHexString(hexString: "#FF4F2F")
        numLabel.textAlignment = .center
        numLabel.layer.masksToBounds = true
        bgView.addSubview(numLabel)
        
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .clear
        gameImageView.layer.cornerRadius = 12*widthScale
        gameImageView.layer.masksToBounds = true
        bgView.addSubview(gameImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "游戏名称", color: "#444444", font: 14*widthScale)
        bgView.addSubview(nameLabel)
        
        typeLabel = HWLabel.createLabel(title: "类型", color: "#888888", font: 10*widthScale)
        bgView.addSubview(typeLabel)
        
        recommendLabel = HWLabel.createBoldLabel(title: "推荐指数", color: "#444444", font: 10*widthScale)
        bgView.addSubview(recommendLabel)
        
        starView = CosmosView.init()
        starView.rating = 0
        starView.settings.filledColor = .colorWithHexString(hexString: "#FF4F2F")
        starView.settings.emptyColor = .colorWithHexString(hexString: "#CCCCCC")
        starView.settings.emptyBorderColor = .clear
        starView.settings.filledBorderColor = .clear
        starView.settings.starSize = 8*widthScale
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
        starView.didFinishTouchingCosmos = {
            rating in
            
        }
        bgView.addSubview(starView)
        
        scaleLabel = HWLabel.createLabel(title: "分成比例：85%", color: "#FFFFFF", font: 8*widthScale)
        scaleLabel.layer.cornerRadius = 6*widthScale
        scaleLabel.backgroundColor = .colorWithHexString(hexString: "#5559EA")
        scaleLabel.textAlignment = .center
        scaleLabel.layer.masksToBounds = true
        bgView.addSubview(scaleLabel)
        
        rebateLabel = HWLabel.createLabel(title: "超高返利", color: "#FFFFFF", font: 8*widthScale)
        rebateLabel.layer.cornerRadius = 6*widthScale
        rebateLabel.backgroundColor = .colorWithHexString(hexString: "#FF4F2F")
        rebateLabel.textAlignment = .center
        rebateLabel.layer.masksToBounds = true
        bgView.addSubview(rebateLabel)
        
        divineLabel = HWLabel.createLabel(title: "满级送神装", color: "#333333", font: 8*widthScale)
        divineLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
        divineLabel.layer.borderWidth = 1
        divineLabel.layer.cornerRadius = 6*widthScale
        divineLabel.textAlignment = .center
        bgView.addSubview(divineLabel)
        
        copyPromoteImageView = UIImageView()
        copyPromoteImageView.image = UIImage(named: "copyPromote")
        bgView.addSubview(copyPromoteImageView)
        
        copyPromoteLabel = HWLabel.createLabel(title: "复制推广", color: "#C1C1CB", font: 12*widthScale)
        bgView.addSubview(copyPromoteLabel)
        
        copyPromoteButton = UIButton(type: .custom)
        copyPromoteButton.addTarget(self, action: #selector(copyPromoteButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(copyPromoteButton)
        
        qrCodeImageView = UIImageView()
        qrCodeImageView.image = UIImage(named: "qrPromote")
        bgView.addSubview(qrCodeImageView)
        
        qrCodeLabel = HWLabel.createLabel(title: "二维码", color: "#C1C1CB", font: 12*widthScale)
        bgView.addSubview(qrCodeLabel)
        
        qrCodeButton = UIButton(type: .custom)
        qrCodeButton.addTarget(self, action: #selector(qrCodeButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(qrCodeButton)
        
        wechatImageView = UIImageView()
        wechatImageView.image = UIImage(named: "wechatPromote")
        bgView.addSubview(wechatImageView)
        
        wechatLabel = HWLabel.createLabel(title: "微信", color: "#C1C1CB", font: 12*widthScale)
        bgView.addSubview(wechatLabel)
        
        wechatButton = UIButton(type: .custom)
        wechatButton.addTarget(self, action: #selector(wechatButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(wechatButton)
        
        qqImageView = UIImageView()
        qqImageView.image = UIImage(named: "qqPromote")
        bgView.addSubview(qqImageView)
        
        qqLabel = HWLabel.createLabel(title: "QQ", color: "#C1C1CB", font: 12*widthScale)
        bgView.addSubview(qqLabel)
        
        qqButton = UIButton(type: .custom)
        qqButton.addTarget(self, action: #selector(qqButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(qqButton)
        
        friendImageView = UIImageView()
        friendImageView.image = UIImage(named: "friendPromote")
        bgView.addSubview(friendImageView)
        
        friendLabel = HWLabel.createLabel(title: "朋友圈", color: "#C1C1CB", font: 12*widthScale)
        bgView.addSubview(friendLabel)
        
        friendButton = UIButton(type: .custom)
        friendButton.addTarget(self, action: #selector(friendButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(friendButton)
        
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.bottom.equalTo(-10*widthScale)
            make.top.equalToSuperview()
            make.height.equalTo(156*widthScale)
        }
        shadowView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview().offset(-5*widthScale)
            make.height.equalTo(10*widthScale)
        }
        
        numLabel.snp.makeConstraints { make in
            make.left.equalTo(14*widthScale)
            make.top.equalTo(35*widthScale)
            make.width.height.equalTo(16*widthScale)
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(numLabel.snp.right).offset(10*widthScale)
            make.top.equalTo(10*widthScale)
            make.width.height.equalTo(58*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(6*widthScale)
            make.top.equalTo(gameImageView).offset(3*widthScale)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        recommendLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(6*widthScale)
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
        }
        
        starView.snp.makeConstraints { make in
            make.left.equalTo(recommendLabel.snp.right).offset(2*widthScale)
            make.centerY.equalTo(recommendLabel)
        }
        
        scaleLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(6*widthScale)
            make.bottom.equalTo(gameImageView)
            make.height.equalTo(12*widthScale)
            make.width.equalTo(70*widthScale)
        }
        
        rebateLabel.snp.makeConstraints { make in
            make.left.equalTo(scaleLabel.snp.right).offset(6*widthScale)
            make.height.equalTo(12*widthScale)
            make.width.equalTo(44*widthScale)
            make.centerY.equalTo(scaleLabel)
        }
        
        divineLabel.snp.makeConstraints { make in
            make.left.equalTo(rebateLabel.snp.right).offset(6*widthScale)
            make.height.equalTo(12*widthScale)
            make.width.equalTo(52*widthScale)
            make.centerY.equalTo(scaleLabel)
        }
        
        copyPromoteImageView.snp.makeConstraints { make in
            make.left.equalTo(24*widthScale)
            make.top.equalTo(shadowView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        copyPromoteLabel.snp.makeConstraints { make in
            make.centerX.equalTo(copyPromoteImageView)
            make.top.equalTo(copyPromoteImageView.snp.bottom).offset(8*widthScale)
        }
        
        qrCodeImageView.snp.makeConstraints { make in
            make.left.equalTo(copyPromoteImageView.snp.right).offset(47*widthScale)
            make.top.equalTo(shadowView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        qrCodeLabel.snp.makeConstraints { make in
            make.centerX.equalTo(qrCodeImageView)
            make.top.equalTo(qrCodeImageView.snp.bottom).offset(8*widthScale)
        }
        
        wechatImageView.snp.makeConstraints { make in
            make.left.equalTo(qrCodeImageView.snp.right).offset(47*widthScale)
            make.top.equalTo(shadowView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        wechatLabel.snp.makeConstraints { make in
            make.centerX.equalTo(wechatImageView)
            make.top.equalTo(wechatImageView.snp.bottom).offset(8*widthScale)
        }
        
        qqImageView.snp.makeConstraints { make in
            make.left.equalTo(wechatImageView.snp.right).offset(47*widthScale)
            make.top.equalTo(shadowView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        qqLabel.snp.makeConstraints { make in
            make.centerX.equalTo(qqImageView)
            make.top.equalTo(qqImageView.snp.bottom).offset(8*widthScale)
        }
        
        friendImageView.snp.makeConstraints { make in
            make.left.equalTo(qqImageView.snp.right).offset(47*widthScale)
            make.top.equalTo(shadowView.snp.bottom).offset(18*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        friendLabel.snp.makeConstraints { make in
            make.centerX.equalTo(friendImageView)
            make.top.equalTo(friendImageView.snp.bottom).offset(8*widthScale)
        }
        
        copyPromoteButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(copyPromoteLabel)
            make.top.equalTo(copyPromoteImageView)
        }
        
        qrCodeButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(qrCodeLabel)
            make.top.equalTo(qrCodeImageView)
        }
        
        wechatButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(wechatLabel)
            make.top.equalTo(wechatImageView)
        }
        
        qqButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(qqLabel)
            make.top.equalTo(qqImageView)
        }
        
        friendButton.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(friendLabel)
            make.top.equalTo(friendImageView)
        }
        
        qqLabel.setContentHuggingPriority(.required, for: .horizontal)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension HWPromoteCell {
    @objc func copyPromoteButtonClick(_:UIButton) -> Void {
        let pboard = UIPasteboard.general
        pboard.string = model?.h5_url
        showHUD(title: "复制成功")
    }
    
    @objc func qrCodeButtonClick(_:UIButton) -> Void {
        let share = HWerweiMaFenXiangView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        share.urlStr = model?.h5_url ?? ""
        currentWindow?.addSubview(share)
    }
    
    @objc func wechatButtonClick(_:UIButton) -> Void {
        if HWGlobalConst.isWechatAndQQ {
            let share = HWPromoteGameViewController()
            share.shared(0, icon: (model?.icon ?? ""), h5Url: (model?.h5_url ?? ""), name: (model?.name ?? ""))
        } else {
            let share = HWerweiMaFenXiangView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
            share.urlStr = model?.h5_url ?? ""
            currentWindow?.addSubview(share)
        }
        
    }

    @objc func qqButtonClick(_:UIButton) -> Void {
        if HWGlobalConst.isWechatAndQQ {
            let share = HWPromoteGameViewController()
            share.shared(1, icon: (model?.icon ?? ""), h5Url: (model?.h5_url ?? ""), name: (model?.name ?? ""))
        } else {
            let share = HWerweiMaFenXiangView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
            share.urlStr = model?.h5_url ?? ""
            currentWindow?.addSubview(share)
        }
        
    }

    @objc func friendButtonClick(_:UIButton) -> Void {
        if HWGlobalConst.isWechatAndQQ {
            let share = HWPromoteGameViewController()
            share.shared(2, icon: (model?.icon ?? ""), h5Url: (model?.h5_url ?? ""), name: (model?.name ?? ""))
        } else {
            let share = HWerweiMaFenXiangView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
            share.urlStr = model?.h5_url ?? ""
            currentWindow?.addSubview(share)
        }
        
    }
}
